package com.example.mongodb.mongodb.controller.rabbitmq.mqttl;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("123.207.179.78");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("admin");
        factory.setPassword("admin");

        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            //正常业务的交换器
            channel.exchangeDeclare("ex.biz",BuiltinExchangeType.DIRECT,true);
            channel.exchangeDeclare("ex.dlx",BuiltinExchangeType.DIRECT,true);
            //生明队列做死信队列
            channel.queueDeclare("queue.dlx",true,false,false,null);
            //绑定死信交换器，死信队列
            channel.queueBind("queue.dlx","ex.dlx","key.dlx");

            //使用direct模式
            Map<String,Object> map = new HashMap<String,Object>();
            //指定消息队列中的消息过期时间
            map.put("x-message-ttl",10*1000);
            map.put("x-expires",60*60*1000);
            //指定过期消息通过死信交换器发送到死信队列，死信交换器的名称DLX
            map.put("x-dead-letter-exchange","ex.dlx");
            //指定死信交换器的路由key
            map.put("x-dead-letter-routing-key","key.dlx");

            channel.queueDeclare("queue.biz",true,false,false,map);
            channel.queueBind("queue.biz","ex.biz","key.biz");

            // 下面参数 2 代表持久化消息
            AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder().contentEncoding("utf-8").deliveryMode(2).build();
            //发送消息
            channel.basicPublish("ex.biz","key.biz",basicProperties,"等待的订单号".getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}
